package com.haixiaoke.mobile.house.controller;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.cleaner.domain.Cleaner;
import com.haixiaoke.saas.cleaner.service.ICleanerService;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.house.domain.HouseSweep;
import com.haixiaoke.saas.house.service.IHouseSweepService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.user.domain.UserConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 打扫记录Controller
 *
 * @author xiaolong
 * @date 2024-05-11
 */
@RestController
@RequestMapping("/mobile/house/sweep")
public class MobileHouseSweepController extends BaseController {
    @Autowired
    private IHouseSweepService houseSweepService;
    @Autowired
    private ICleanerService cleanerService;

    /**
     * 查询打扫记录列表
     */
    @GetMapping("/list")
    @TenantScope
    @AuthCheck
    public TableDataInfo list(HouseSweep houseSweep) {
        startPage();
        Long userId = getUserId();
        Cleaner cleaner = cleanerService.selectCleanerByCleanerUserId(userId, TenantContext.getTenantId());
        if (ObjectUtil.isEmpty(cleaner)) {
            throw new ServiceException("当前用户未关联保洁人员");
        }
        houseSweep.setCleanerId(cleaner.getCleanerId());
        houseSweep.setTenantId(TenantContext.getTenantId());
        List<HouseSweep> list = houseSweepService.selectHouseSweepList(houseSweep);
        return getDataTable(list);
    }


    /**
     * 获取打扫记录详细信息
     */
    @GetMapping(value = "/{houseSweepId}")
    @AuthCheck
    public AjaxResult getInfo(@PathVariable("houseSweepId") String houseSweepId) {
        return success(houseSweepService.selectHouseSweepByHouseSweepId(houseSweepId));
    }

    /**
     * 新增打扫记录
     */
    @PutMapping
    @TenantScope
    @RepeatSubmit
    @AuthCheck
    public AjaxResult edit(@RequestBody HouseSweep houseSweep) {
        Long userId = getUserId();
        Cleaner cleaner = cleanerService.selectCleanerByCleanerUserId(userId, TenantContext.getTenantId());
        if (ObjectUtil.isEmpty(cleaner)) {
            throw new ServiceException("当前用户未关联保洁人员");
        }
        houseSweep.setCleanerId(cleaner.getCleanerId());
        houseSweep.setTenantId(TenantContext.getTenantId());
        houseSweep.setCleanerUserId(userId);
        houseSweep.setUpdateBy(getUsername());
        houseSweep.setOperatorId(userId);
        return toAjax(houseSweepService.editHouseSweepList(houseSweep));

    }
}
